package com.wwwarehouse.dubbo;

import com.alibaba.dubbo.config.*;
import com.alibaba.dubbo.remoting.http.servlet.BootstrapListener;
import com.alibaba.dubbo.remoting.http.servlet.DispatcherServlet;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.boot.web.servlet.ServletListenerRegistrationBean;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
@EnableConfigurationProperties(DubboProperties.class)
public class MiddlewareConfig {

    @Bean
    public ServletRegistrationBean restServlet(){
        DispatcherServlet dispatcherServlet = new DispatcherServlet();
        //用ServletRegistrationBean包装servlet
        ServletRegistrationBean registrationBean = new ServletRegistrationBean(dispatcherServlet);
        registrationBean.setLoadOnStartup(1);
        //指定urlmapping
        registrationBean.addUrlMappings("/rest/*");
        //指定name，如果不指定默认为dispatcherServlet
        registrationBean.setName("rest");
        return registrationBean;
    }

    @Bean
    public ServletListenerRegistrationBean restListener(){
        BootstrapListener bootstrapListener = new BootstrapListener();
        ServletListenerRegistrationBean registrationBean = new ServletListenerRegistrationBean(bootstrapListener);
        registrationBean.setEnabled(true);

        return registrationBean;
    }

    @Bean
    public ApplicationConfig requestApplicationConfig(DubboProperties dubboProperties) {
        ApplicationConfig applicationConfig = dubboProperties.getApplication();
        if (applicationConfig == null) {
            applicationConfig = new ApplicationConfig();
        }
        return applicationConfig;
    }

    @Bean
    public RegistryConfig requestRegistryConfig(DubboProperties dubboProperties) {
        RegistryConfig registryConfig = dubboProperties.getRegistry();
        if (registryConfig == null) {
            registryConfig = new RegistryConfig();
        }
        return registryConfig;
    }

    @Bean
    public ProtocolConfig requestProtocolConfig(DubboProperties dubboProperties) {
        ProtocolConfig protocolConfig = dubboProperties.getProtocol();
        if (protocolConfig == null) {
            protocolConfig = new ProtocolConfig();
        }
        return protocolConfig;
    }

    @Bean
    public MonitorConfig requestMonitorConfig(DubboProperties dubboProperties) {
        MonitorConfig monitorConfig = dubboProperties.getMonitor();
        if (monitorConfig == null) {
            monitorConfig = new MonitorConfig();
        }
        return monitorConfig;
    }

    @Bean
    public ProviderConfig requestProviderConfig(DubboProperties dubboProperties) {
        ProviderConfig providerConfig = dubboProperties.getProvider();
        if (providerConfig == null) {
            providerConfig = new ProviderConfig();
        }
        return providerConfig;
    }

    @Bean
    public ModuleConfig requestModuleConfig(DubboProperties dubboProperties) {
        ModuleConfig moduleConfig = dubboProperties.getModule();
        if (moduleConfig == null) {
            moduleConfig = new ModuleConfig();
        }
        return moduleConfig;
    }

    @Bean
    public MethodConfig requestMethodConfig(DubboProperties dubboProperties) {
        MethodConfig methodConfig = dubboProperties.getMethod();
        if (methodConfig == null) {
            methodConfig = new MethodConfig();
        }
        return methodConfig;
    }

    @Bean
    public ConsumerConfig requestConsumerConfig(DubboProperties dubboProperties) {
        ConsumerConfig consumerConfig = dubboProperties.getConsumer();
        if (consumerConfig == null) {
            consumerConfig = new ConsumerConfig();
        }
        return consumerConfig;
    }
}
